﻿using Core.Dtos;
using Core.Entities;
using Core.IRepository;
using Core.Messages;
using Core.Queries;
using Core.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Microsoft.AspNet.Identity;

namespace Front.Controllers.Api
{
    public class ArticleApiController : CrudBaseController<int, Article, ArticleDto, ArticleQuery>
    {
        IColumnRepository ColumnRepository;
        IItemValueRepository ItemValueRepository;
        IItemRepository ItemRepository;

        public ArticleApiController(IUnitOfWork UnitOfWork, IArticleRepository ArticleRepository, IColumnRepository ColumnRepository, IItemValueRepository ItemValueRepository, IItemRepository ItemRepository)
            : base(UnitOfWork, ArticleRepository)
        {
            this.ColumnRepository = ColumnRepository;
            this.ItemValueRepository = ItemValueRepository;
            this.ItemRepository = ItemRepository;
        }

        [HttpPost]
        [Route("api/articleapi/findby")]
        public ListResponse<ArticleDto> FindBy(BaseRequest<ArticleQuery> request)
        {
           
            return base.DefaultFindBy(request.Value);
        }

        [HttpPost]
        [Route("api/articleapi/findnamesby")]
        public ListResponse<ArticleListVM> FindNamesBy(BaseRequest<ArticleQuery> request)
        {
            ArticleQuery query = request.Value;
            query.UserID = User.Identity.GetUserId();            
            return base.DefaultFindBy<ArticleListVM>(request.Value);
        }

        public BaseResponse<int> Post(BaseRequest<ArticleDto> request)
        {
            BaseResponse<int> response = new BaseResponse<int>();

            ArticleDto article = request.Value;
            article.UserID = User.Identity.GetUserId();
            article.TypeID = 1;
            response = base.DefaultAdd(article);
            
            Column column = new Column() { ArticleID = response.Value, Name = "Message" };
            ColumnRepository.Add(column);
            UnitOfWork.Commit();
            
            return response;
        }

        public BaseResponse Put(int id, BaseRequest<ArticleDto> request)
        {
            return base.DefaultEdit(id, request.Value);
        }

        public override BaseResponse Delete(int id)
        {
            BaseResponse response = new BaseResponse();
            try
            {
                Article entity = Repository.FindBy(id);
                ItemValueRepository.Delete(x => x.Column.ArticleID == id);
                ItemRepository.Delete(x => x.ArticleID == id);
                ColumnRepository.Delete(x => x.ArticleID == id);                
                Repository.Delete(entity);                
                UnitOfWork.Commit();
                response.Success = true;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ErrorMessage = ex.ToString();
            }

            return response;
        }
    }
}
